home *** CD-ROM | disk | FTP | other *** search
/ The Complete Utilities To…ka 501 Killer Utilities! / 501 Killer Utilities! (Macworld July 1995).cdr / Programming / OutOfPhase1.1 Source / OutOfPhase Folder / FilterSecondOrderReson.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-03  |  4.5 KB  |  154 lines  |  [TEXT/KAHL]

  1. /* FilterSecondOrderReson.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. /* Based on material from pages 184-190 of */
  26. /* Dodge, Charles and Jerse, Thomas A. */
  27. /* Computer Music:  Synthesis, Composition, and Performance */
  28. /* Schirmer Books, New York, 1985 */
  29.  
  30. #include "MiscInfo.h"
  31. #include "Audit.h"
  32. #include "Debug.h"
  33. #include "Definitions.h"
  34.  
  35. #include "FilterSecondOrderReson.h"
  36. #include "Memory.h"
  37. #include "FloatingPoint.h"
  38.  
  39.  
  40. struct SecondOrderResonRec
  41.     {
  42.         /* link */
  43.         SecondOrderResonRec*        Next;
  44.  
  45.         /* state variables */
  46.         float                                        Ym1;
  47.         float                                        Ym2;
  48.  
  49.         /* coefficients */
  50.         float                                        A0;
  51.         float                                        B1;
  52.         float                                        B2;
  53.     };
  54.  
  55.  
  56. static SecondOrderResonRec*        FreeList = NIL;
  57.  
  58.  
  59. /* flush free list */
  60. void                                            FlushCachedSecondOrderResonStuff(void)
  61.     {
  62.         while (FreeList != NIL)
  63.             {
  64.                 SecondOrderResonRec*        Temp;
  65.  
  66.                 Temp = FreeList;
  67.                 FreeList = FreeList->Next;
  68.                 ReleasePtr((char*)Temp);
  69.             }
  70.     }
  71.  
  72.  
  73. /* create a new filter record */
  74. SecondOrderResonRec*            NewSecondOrderReson(void)
  75.     {
  76.         SecondOrderResonRec*        Filter;
  77.  
  78.         if (FreeList != NIL)
  79.             {
  80.                 Filter = FreeList;
  81.                 FreeList = FreeList->Next;
  82.             }
  83.          else
  84.             {
  85.                 Filter = (SecondOrderResonRec*)AllocPtrCanFail(sizeof(SecondOrderResonRec),
  86.                     "SecondOrderResonRec");
  87.                 if (Filter == NIL)
  88.                     {
  89.                         return NIL;
  90.                     }
  91.             }
  92.         Filter->Ym1 = 0;
  93.         Filter->Ym2 = 0;
  94.         return Filter;
  95.     }
  96.  
  97.  
  98. /* dispose filter record */
  99. void                                            DisposeSecondOrderReson(SecondOrderResonRec* Filter)
  100.     {
  101.         CheckPtrExistence(Filter);
  102.         Filter->Next = FreeList;
  103.         FreeList = Filter;
  104.     }
  105.  
  106.  
  107. /* adjust filter coefficients */
  108. void                                            SetSecondOrderResonCoefficients(SecondOrderResonRec* Filter,
  109.                                                         float Cutoff, float Bandwidth, FilterScalings Scaling,
  110.                                                         long SamplingRate)
  111.     {
  112.         CheckPtrExistence(Filter);
  113.         Filter->B2 = DEXP(-6.28318530717958648 * Bandwidth / SamplingRate);
  114.         Filter->B1 = ((-4 * Filter->B2) / (1 + Filter->B2))
  115.             * DCOS(6.28318530717958648 * Cutoff / SamplingRate);
  116.         switch (Scaling)
  117.             {
  118.                 default:
  119.                     EXECUTE(PRERR(ForceAbort,"SetSecondOrderResonCoefficients:  unknown scaling"));
  120.                     break;
  121.                 case eFilterDefaultScaling:
  122.                     Filter->A0 = 1;
  123.                     break;
  124.                 case eFilterResonMidbandGain1:
  125.                     Filter->A0 = (1 - Filter->B2) * DSQRT(1 - Filter->B1 * Filter->B1
  126.                         / (4 * Filter->B2));
  127.                     break;
  128.                 case eFilterResonNoiseGain1:
  129.                     {
  130.                         float                                        X;
  131.                         float                                        Y;
  132.  
  133.                         X = 1 + Filter->B2;
  134.                         Y = 1 - Filter->B2;
  135.                         Filter->A0 = DSQRT( (X * X - Filter->B1 * Filter->B1)
  136.                             * Y / X);
  137.                     }
  138.                     break;
  139.             }
  140.     }
  141.  
  142.  
  143. /* apply filter to a sample value */
  144. float                                            ApplySecondOrderReson(SecondOrderResonRec* Filter, float Xin)
  145.     {
  146.         float                                        Y;
  147.  
  148.         CheckPtrExistence(Filter);
  149.         Y = Filter->A0 * Xin - Filter->B1 * Filter->Ym1 - Filter->B2 * Filter->Ym2;
  150.         Filter->Ym2 = Filter->Ym1;
  151.         Filter->Ym1 = Y;
  152.         return Y;
  153.     }
  154.